package com.smart.car.member.feign.fallback;

import com.smart.car.common.res.bean.ResponseResult;
import com.smart.car.common.res.entity.PointsInfo;
import com.smart.car.common.res.exception.BusinessException;
import com.smart.car.member.feign.PointsFeignService;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;

/**
 * 快速失败方案,【工厂模式】。已经改为Sentinel的fallback处理。
 * @author: zoutao
 * @date: 2021-04-25
 */
//@Component
@Slf4j
public class ProductCenterFeignApi implements FallbackFactory<PointsFeignService> {
    @Override
    public PointsFeignService create(Throwable throwable) {
        ResponseResult<Object> result = new ResponseResult<>();

        return new PointsFeignService(){
            @Override
            public ResponseResult getUserPoints(String id) {
                log.error("远程调用失败原因: ", throwable);
                if(throwable instanceof com.alibaba.csp.sentinel.slots.block.flow.FlowException) {
                    result.setMsg("接口被流控了！");
                } else if(throwable instanceof com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) {
                    result.setMsg("服务被降级了！");
                }
                else {
                    result.setMsg("其他异常了！");
                }
                return result;
            }

            @Override
            public ResponseResult<Integer> addPoints(String json) throws BusinessException {
                return null;
            }

            @Override
            public ResponseResult<Integer> updateCard(String json) throws BusinessException {
                return null;
            }

            @Override
            public ResponseResult getPointsById(String pointsId) {
                return null;
            }

            @Override
            public ResponseResult getPointsByPhone(String phone) {
                return null;
            }

            @Override
            public ResponseResult<PointsInfo> getData(String memberId) {
                return null;
            }

        };
    }
}
